<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Log;
use app\service\apixcxService;
use app\service\imageService;
use app\service\wotuService;
use app\service\deviceService;
class Uface extends Controller
{
	public function piclist()
	{
		$user_id=request()->post("user_id");
		$userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => '您还未绑定房间，无法访问']);
		}
        if($userinfo["personGuid"]=="0")
        {
            
            $type="1";         
            $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$type,$userinfo["ryid"],$userinfo["idcard"]);
            
            $result=json_decode($re,true);
            
            $guid=$result["data"]["guid"];
            Db::table("personnel")->where("user_id",$user_id)->update(["personGuid"=>$guid]);
            $userinfo["personGuid"]=$guid;
        }
        $data["userinfo"]=$userinfo;
        $res=(new wotuService())->tupian($userinfo["personGuid"]);
        //Log::write($res,"res");
        $data["pic"]=$res["data"];
        $num=count($res["data"]);
        $data["num"]=$num;
        $data["uploadstatus"]=true;
        if($num=="3"){
            $data["uploadstatus"]=false;
        }
        return json(["errCode"=>"0",'datainfo' =>$data]);
	}
	public function familypiclist()
	{
		$pid=request()->post("pid");
		$userinfo=Db::table("personnel")->where("id",$pid)->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => '用户信息不存在']);
		}
        if($userinfo["personGuid"]=="0")
        {
            
            $type=$userinfo["relation"];         
            $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$type,$userinfo["ryid"],$userinfo["idcard"]);
            
            $result=json_decode($re,true);
            
            $guid=$result["data"]["guid"];
            Db::table("personnel")->where("id",$pid)->update(["personGuid"=>$guid]);
            $userinfo["personGuid"]=$guid;
        }
        $data["userinfo"]=$userinfo;
        $res=(new wotuService())->tupian($userinfo["personGuid"]);
        $data["pic"]=$res["data"];
        $num=count($res["data"]);
        $data["num"]=$num;
        $data["uploadstatus"]=true;
        if($num=="3"){
            $data["uploadstatus"]=false;
        }
        return json(["errCode"=>"0",'datainfo' =>$data]);
	}
	public function uploadimage(){
		$user_id=request()->post("user_id");
		$image=request()->post("image");
		//$image="";
		//$user_id="2088112618452297";
		$userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
		$result=(new wotuService())->uploadimgurl($image,$userinfo["personGuid"]);
	    Log::write($result,"上传图片");
		
		/*$url = strlen('https://'.$_SERVER['HTTP_HOST'].'/');
　　　　$ifurl = strpos($image,'uploads');
　　　　unlink(substr($image, $url));*/
        if($result["code"]=="GS_SUS600"){
            //查询该小区下的人脸设备并授权
            $locklist=(new deviceService())->userdeviceList($user_id);
            $deviceKeys="";
            if(empty($locklist)){
                return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
            }
            foreach ($locklist as $key => $value) {
                $deviceKeys=$deviceKeys.$value["devicekey"].",";
            }
            $deviceKeys=rtrim($deviceKeys,",");
            $res=(new wotuService())->accredit($userinfo["personGuid"], $deviceKeys);
            Log::write($res,"123");
             return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
        }else{
            return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
        }
	}
	public function familyuploadimage(){
		$pid=request()->post("pid");
		$image=request()->post("image");
		//$image="";
		//$user_id="2088112618452297";
		$userinfo=Db::table("personnel")->where("id",$pid)->find();
		$result=(new wotuService())->uploadimgurl($image,$userinfo["personGuid"]);
	
		
		/*$url = strlen('https://'.$_SERVER['HTTP_HOST'].'/');
　　　　$ifurl = strpos($image,'uploads');
　　　　unlink(substr($image, $url));*/
        if($result["code"]=="GS_SUS600"){
            //查询该小区下的人脸设备并授权
            $locklist=(new deviceService())->userdeviceList($userinfo["parent_user_id"]);
            $deviceKeys="";
            if(empty($locklist)){
                return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
            }
            foreach ($locklist as $key => $value) {
                $deviceKeys=$deviceKeys.$value["devicekey"].",";
            }
            $deviceKeys=rtrim($deviceKeys,",");
            (new wotuService())->accredit($userinfo["personGuid"], $deviceKeys);
            
             return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
        }else{
            return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
        }
	}
    //上传图片处理
    public function uploadAction()
    {
        $user_id=request()->post("user_id");
        $zim_id=request()->post("zim_id");
        $data["biz_id"]=$user_id;
        $data["zim_id"]=$zim_id;
        $api=new apixcxService();
        
        $res=$api->faceimgquery(json_encode($data));
        
        if($res->code=="10000"){
            //查询成功
            $rest=$res->extern_info;
            $re=json_decode($rest,true);
            $img=$re["imgStr"];
            $image=$this->urlsafe_b64decode($img);
            $userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
            $guid=$userinfo["personGuid"];
            $result=(new wotuService())->uploadimg($image,$guid);
            if($result["code"]=="GS_SUS600"){
                //查询该小区下的人脸设备并授权
                $locklist=(new deviceService())->userdeviceList($user_id);
                $deviceKeys="";
                if(empty($locklist)){
                    return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
                }
                foreach ($locklist as $key => $value) {
                    $deviceKeys=$deviceKeys.$value["devicekey"].",";
                }
                $deviceKeys=rtrim($deviceKeys,",");
                $re=(new wotuService())->accredit($guid, $deviceKeys);
                Log::write($re,"授权");
                 return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
            }else{
                return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
            }
        }
    }
    public function delimage(){
        $guid=request()->post("guid");
        $user_id=request()->post("user_id");
        $userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
        if(empty($userinfo))
        {
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        if(empty($userinfo["personGuid"])){
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        $personGuid=$userinfo["personGuid"];
        $result=(new wotuService())->delimage($personGuid,$guid);
        //图片删除成功
        if($result["code"]=="GS_SUS601"){
            return json(["errCode"=>"1",'msg' => '图片删除成功']);
            
        }else{
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
    }

    public function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return $data;
       return base64_decode($data);
    }
}